package org.kim.dao;

import org.kim.core.StorageCache;
import org.kim.pojo.Book;
import org.kim.pojo.Borrow;

import java.util.Optional;
import java.util.Set;

public class BorrowDao extends BaseDao<Borrow> {

    public boolean bookIsBorrowed(Book book) {
        Set<String> keySet = StorageCache.borrowCache.keySet();
        boolean contains = false;
        Optional<String> key = keySet.stream()
                .filter(e -> e.contains(book.getId())).findAny();
        return key.isPresent() ? true : false;
    }

    public boolean addBorrow(Borrow borrow) throws Exception {

        StorageCache.borrowCache.put(borrow.getAccountId() + "_" + borrow.getBookId(), borrow);
        super.persistence(borrow, borrow.getAccountId() + "_" + borrow.getBookId());
        return true;
    }

    public boolean updateBorrow(Borrow borrow) {

        StorageCache.borrowCache.put(borrow.getAccountId() + "_" + borrow.getBookId(), borrow);
        return true;
    }
}
